<template>
	<view>
		<view class="city" @touchmove.stop.prevent="moveStop">
			<!-- #ifdef H5  -->
			<view class="city_close" @click="close_city"><uni-icons type="closeempty" size="20"></uni-icons></view>
			<!-- #endif -->
			<view class="city_title">当前城市</view>
			<view class="city_title_row">
				<view class="city_title_row_name">{{ map.city || '城市' }}</view>
				<view class="city_title_row_btn" @click="reposition">自动获取</view>
			</view>
		</view>
		<view class="indexed">
			<uni-indexed-list :options="city_list" :showSelect="false" @click="bindClick"></uni-indexed-list>
		</view>
	</view>
</template>

<script>
import Map from '../../utils/map.js';
import Route from '../../utils/route.js';
export default {
	data() {
		return {
			map: {
				city: "",
				city_code: '',
				district: "",
				district_code: "",
				latitude: 0,
				longitude: 0
			},
			// 城市列表
			city_list: []
		};
	},
	async onLoad() {
		this.map = uni.getStorageSync('map');
		
	},
	async onReady() {
		await this.getCityList();
	},
	methods: {
		// 获取城市数据
		async getCityList(){
			uni.showLoading();
			this.city_list = await Route.city_list();
			this.city_list = this.city_list;
			uni.hideLoading();
		},
		// 关闭
		close_city() {
			uni.navigateBack();
		},
		// 重新定位
		async reposition() {
			uni.showToast({
				title: '正在定位',
				duration: 1500,
				icon: 'loading'
			});
			// #ifdef MP-WEIXIN
			this.map = await Map.wechat();
			// #endif
			// #ifdef H5
			this.map = await Map.H5();
			// #endif
			this.map.district = '';
			this.map.district_code = '';
			uni.setStorageSync('map', this.map);
			uni.showToast({
				title: '定位成功',
				duration: 1500,
				icon: 'none'
			});
		},
		// 手动选择城市
		bindClick(e) {
			if(!e.item.name.regionName){
				e.item = e.items;
			}
			this.map= {
				city: "",
				city_code: '',
				district: "",
				district_code: "",
				latitude: 0,
				longitude: 0
			}
			this.map.city = e.item.name.regionName;
			this.map.city_code = e.item.name.cityId;
			this.map.district = '';
			this.map.district_code = '';
			uni.setStorageSync('map', this.map);
			uni.navigateBack()
		}
	}
};
</script>

<style lang="scss">
	page{
		position: fixed;
	}
.city {
	position: fixed;
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	width: 690rpx;
	height: 150rpx;
	left: 0;
	right: 0;
	margin: 0 auto;
	
	&_close {
		position: absolute;
		right: 0;
		top: 10rpx;
		padding: 10rpx 20rpx;
	}
	&_title {
		font-size: $uni-font-size-lg;
		font-weight: 600;
		&_row {
			display: flex;
			align-items: center;
			&_name {
				font-size: $uni-font-size-lg;
				background: $template-background;
				color: $uni-text-color-inverse;
				padding: 6rpx 33rpx;
				text-align: center;
				border-radius: 40rpx;
			}
			&_btn {
				font-size: $uni-font-size-lg;
				background: $template-background;
				color: $uni-text-color-inverse;
				padding: 6rpx 33rpx;
				text-align: center;
				border-radius: 40rpx;
				margin-left: 33rpx;
			}
		}
	}
}
.indexed {
	height: calc(100vh - 150rpx);
}
</style>
